package com.vecspace.scheduler.job;

import java.util.Date;

import com.vecspace.utils.log.HLogger;

public abstract class AbstractJob implements TJob{
	protected String jobName;
	//是否可以并行执行（默认不能并行执行,当发生并行请求时，后到的请求被忽略）
	protected boolean concurrencyExcute = false;
	protected boolean executing = false;
	protected Date lastRuningStartTime;
	protected Date lastRuningEndTime;
	
	@Override
	public boolean executing() {
		return executing;
	}

	public void doExecute(String args) {
		if(executing && !concurrencyExcute)return;//同一个job不能并行执行,当发生并行请求时，后到的请求被忽略
		executing = true;
		try{
			lastRuningStartTime = new Date();
			lastRuningEndTime = lastRuningStartTime;
			HLogger.DEFAULT().info("开始执行任务:"+getJobName()+","+this.getClass().getName());
			this.execute(args);
			lastRuningEndTime = new Date();
			HLogger.DEFAULT().info("结束执行任务:"+getJobName()+","+this.getClass().getName()+"\r\n");
		}finally{
			executing = false;
		}
	}

	@Override
	public Date getLastRuningStartTime() {
		return lastRuningStartTime;
	}

	@Override
	public Date getLastRuningEndTime() {
		return lastRuningEndTime;
	}

	@Override
	public Object executeCommand(String cmd, String args) {
		if("runOnce".equalsIgnoreCase(cmd)){
			doExecute(args);
			return true;
		}
		return null;
	}

	@Override
	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}
}
